package top.kaoshanji.p1ch05f;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import top.kaoshanji.p1ch05f.config.ProjectConfig;
import top.kaoshanji.p1ch05f.repositories.CommentRepository;
import top.kaoshanji.p1ch05f.services.CommentService;
import top.kaoshanji.p1ch05f.services.UserService;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/4/27 21:07
 */
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { ProjectConfig.class })
public class AppTests {

    @Autowired
    private ApplicationContext context;

    /**
     * 代码清单test-p1ch05f-01：每次从上下文中获取原型bean都是不同的
     */
    @Test
    public void testCommentRepositoryIsPrototype() {

        var cr1 = context.getBean("commentRepository", CommentRepository.class);
        var cr2 = context.getBean("commentRepository", CommentRepository.class);

        Assertions.assertNotEquals(cr1, cr2);

    }

    /**
     * 代码清单test-p1ch05f-02：从上下文中获取单例bean（单例bean依赖原型bean），原型bean是不同的对象
     */
    @Test
    public void testPrototypeBeanInjection() {
        var s1 = context.getBean("commentService", CommentService.class);
        var s2 = context.getBean("userService", UserService.class);

        Assertions.assertNotEquals(s1.getCommentRepository(), s2.getCommentRepository());

    }



}
